package com.yb.custom.annotation.log.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yb.custom.annotation.log.entity.OperateLog;
import com.yb.custom.annotation.log.mapper.OperateLogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * OperateLog表服务实现类
 *
 * @author yangbiao
 * @since 2022-03-30 15:24:24
 */
@Service
@RequiredArgsConstructor
public class OperateLogService extends ServiceImpl<OperateLogMapper, OperateLog> {

    /**
     * 查询分页列表信息
     * @param page
     * @param size
     * @param logType
     * @param requestType
     * @param userOrganization
     * @param userDepartment
     * @param userPosition
     * @param userFullName
     * @param userRole
     * @param operateUsername
     * @param operateIp
     * @param operateContent
     * @param operateTimeStart
     * @param operateTimeEnd
     * @return
     */
    public IPage<OperateLog> queryPageInfo(Integer page, Integer size, Integer logType, String requestType,
                                           String userOrganization, String userDepartment, String userPosition,
                                           String userFullName, String userRole, String operateUsername, String operateIp,
                                           String operateContent,LocalDateTime operateTimeStart, LocalDateTime operateTimeEnd) {
        final Page<OperateLog> pageInfo = new Page<>(page, size);
        final LambdaQueryWrapper<OperateLog> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(OperateLog::getOperateTime);
        wrapper.eq(Objects.nonNull(logType),OperateLog::getLogType,logType);
        wrapper.eq(StrUtil.isNotBlank(requestType),OperateLog::getRequestType,requestType);
        wrapper.eq(StrUtil.isNotBlank(userOrganization),OperateLog::getUserOrganization,userOrganization);
        wrapper.eq(StrUtil.isNotBlank(userDepartment),OperateLog::getUserDepartment,userDepartment);
        wrapper.eq(StrUtil.isNotBlank(userPosition),OperateLog::getUserPosition,userPosition);
        wrapper.like(StrUtil.isNotBlank(userFullName),OperateLog::getUserFullName,userFullName);
        wrapper.eq(StrUtil.isNotBlank(userRole),OperateLog::getUserRole,userRole);
        wrapper.eq(StrUtil.isNotBlank(operateUsername),OperateLog::getOperateUsername,operateUsername);
        wrapper.likeRight(StrUtil.isNotBlank(operateIp),OperateLog::getOperateIp,operateIp);
        wrapper.like(StrUtil.isNotBlank(operateContent),OperateLog::getOperateContent,operateContent);
        wrapper.ge(Objects.nonNull(operateTimeStart),OperateLog::getOperateTime,operateTimeStart);
        wrapper.le(Objects.nonNull(operateTimeEnd),OperateLog::getOperateTime,operateTimeEnd);
        final IPage<OperateLog> result = this.page(pageInfo, wrapper);
        return result;
    }

}
